home *** CD-ROM | disk | FTP | other *** search
/ Amiga Plus Special 25 / AMIGAplus Sonderheft 25 (2000)(Falke)(DE)(Track 1 of 4)[!].iso / Updates / PowerPC / pdflib / clients / pdfgraph.c < prev    next >
C/C++ Source or Header  |  2000-05-16  |  4KB  |  169 lines

  1. /*---------------------------------------------------------------------------*
  2.  |        PDFlib - A library for dynamically generating PDF files            |
  3.  +---------------------------------------------------------------------------+
  4.  |        Copyright (c) 1997-1999 Thomas Merz. All rights reserved.          |
  5.  +---------------------------------------------------------------------------+
  6.  |    This software is not in the public domain.  It is subject to the       |
  7.  |    "Aladdin Free Public License".  See the file license.txt for details.  |
  8.  |    This license grants you the right to use and redistribute PDFlib       |
  9.  |    under certain conditions. Among other things, the license requires     |
  10.  |    that the copyright notice and this notice be preserved on all copies.  |
  11.  |    This requirement extends to ports to other programming languages.      |
  12.  |                                                                           |
  13.  |    In short, you are allowed to develop and use PDFlib-based software     |
  14.  |    as long as you don't sell it. Commercial use of PDFlib requires a      |
  15.  |    commercial license which can be obtained from the author of PDFlib.    |
  16.  |    Contact information can be found in the accompanying PDFlib manual.    |
  17.  |    PDFlib is distributed with no warranty of any kind. Commercial users,  |
  18.  |    however, will receive warranty and support statements in writing.      |
  19.  *---------------------------------------------------------------------------*/
  20.  
  21. /* pdfgraph.c
  22.  *
  23.  * A micro language for drawing PDF graphics
  24.  *
  25.  */
  26.  
  27. #include <stdio.h>
  28. #include <string.h>
  29. #include <stdlib.h>
  30.  
  31. #if !defined(WIN32) && !defined(MAC)
  32. #include <unistd.h>
  33. #endif
  34.  
  35. #ifdef WIN32
  36. #include <process.h>
  37. #endif
  38.  
  39. #ifdef NeXT
  40. #include <libc.h>    /* for getopt(), optind, optarg */
  41. #endif
  42.  
  43. #ifdef __CYGWIN32__
  44. #include <getopt.h>    /* for getopt(), optind, optarg */
  45. #endif
  46.  
  47. #include "pdflib.h"
  48. #include "p_config.h"
  49.  
  50. static void
  51. usage(void)
  52. {
  53.     fprintf(stderr, "pdfgraph - draw PDF graph. (C) Thomas Merz 1997-99\n");
  54.     fprintf(stderr, "usage: pdfgraph [options] [datafile]\n");
  55.     fprintf(stderr, "Available options:\n");
  56.     fprintf(stderr, "-b         binary mode (default: ASCII)\n");
  57.     fprintf(stderr, "-o filename    PDF output file name\n");
  58.  
  59.     exit(1);
  60. }
  61.  
  62. #define BUFLEN 512
  63.  
  64. int
  65. main(int argc, char *argv[])
  66. {
  67.     char    buf[BUFLEN], *cmd;
  68.     char    *pdffilename = NULL;
  69.     FILE    *datafile = stdin;
  70.     PDF        *p;
  71.     int        opt;
  72.     float    page_width = 595, page_height = 842;
  73.     float    x, y, gray;
  74.     float    red, green, blue;
  75.  
  76.     while ((opt = getopt(argc, argv, "o:")) != -1)
  77.     switch (opt) {
  78.         case 'o':
  79.         pdffilename = optarg;
  80.         break;
  81.  
  82.         case '?':
  83.         default:
  84.         usage();
  85.     }
  86.  
  87.     if (pdffilename == NULL)
  88.     usage();
  89.  
  90.     if (optind < argc) {
  91.     if ((datafile = fopen(argv[optind], READMODE)) == NULL) {
  92.         fprintf(stderr, "Error: cannot open data file %s.\n",argv[optind]);
  93.         exit(1);
  94.     }
  95.     } else
  96.     usage();
  97.  
  98.     p = PDF_new();
  99.     if (p == NULL) {
  100.     fprintf(stderr, "Error: cannot open output file %s.\n", pdffilename);
  101.     usage();
  102.     }
  103.  
  104.     PDF_open_file(p, pdffilename);
  105.  
  106.     PDF_set_info(p, "Title", "Converted graphics");
  107.     PDF_set_info(p, "Creator", "pdfgraph");
  108.  
  109.     PDF_begin_page(p, page_width, page_height);
  110.  
  111.     while ((cmd = fgets(buf, BUFLEN, datafile)) != NULL) {
  112.     switch (cmd[0]) {
  113.     case 'M':
  114.         if (sscanf(buf+1, "%f %f", &x, &y) != 2) {
  115.         fprintf(stderr, "Error in line: %s", buf);
  116.         continue;
  117.         }
  118.         PDF_moveto(p, x, y);
  119.         break;
  120.  
  121.     case 'L':
  122.         if (sscanf(buf+1, "%f %f", &x, &y) != 2) {
  123.         fprintf(stderr, "Error in line: %s", buf);
  124.         continue;
  125.         }
  126.         PDF_lineto(p, x, y);
  127.         break;
  128.  
  129.     case 'S':
  130.         PDF_stroke(p);
  131.         break;
  132.  
  133.     case 'f':
  134.         PDF_fill(p);
  135.         break;
  136.  
  137.     case 'F':
  138.         PDF_fill_stroke(p);
  139.         break;
  140.  
  141.     case 'g':
  142.         if (sscanf(buf+1, "%f", &gray) != 1) {
  143.         fprintf(stderr, "Error in line: %s", buf);
  144.         continue;
  145.         }
  146.         PDF_setgray(p, gray);
  147.         break;
  148.  
  149.     case 'C':
  150.         if (sscanf(buf+1, "%f %f %f", &red, &green, &blue) != 3) {
  151.         fprintf(stderr, "Error in line: %s", buf);
  152.         continue;
  153.         }
  154.         PDF_setrgbcolor(p, red, green, blue);
  155.         break;
  156.  
  157.     case '%':
  158.     default:
  159.         break;;
  160.     }
  161.     }
  162.  
  163.     PDF_end_page(p);
  164.     PDF_close(p);
  165.  
  166.     fclose(datafile);
  167.     exit(0);
  168. }
  169.